<?php

//Includes
require_once("MasterPage.php");
require_once("vcl/vcl.inc.php");
require_once("AdminLogin.php");
require_once("DB.php");
use_unit("pager.inc.php");
use_unit("db.inc.php");
use_unit("dbctrls.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");
use_unit("rtl.inc.php");

//Class definition
class Search extends MasterPage
{

   public $lblInfo = null;
   public $pnlSearch = null;
   public $pgResults = null;
   public $pnlPager = null;
   public $lblResults = null;
   public $pnResults = null;
   public $btSearch = null;
   public $edSearch = null;

   function SearchCreate($sender, $params)
   {

      $records = GetSessionVar('records');
      $this->pgResults->DesignTotalRecords = $records;

   }

   function SearchBeforeShow($sender, $params)
   {

      global $DB;
      $page = 1; // Default to page 1

      //Get the query term
      if(empty($this->edSearch->Text))
      {
         if(!isset($_REQUEST['query']))
         {
            $query = GetSessionVar('query');
         }
         else
         {
            $query = $_REQUEST['query'];
            SetSessionVar("query", $query);
         }
         $this->edSearch->Text = $query;
      }
      else
      {
        $query = $this->edSearch->Text;
        SetSessionVar("query", $query);
      }

      // Clear the list
      $this->lblResults->Caption = "";
      // hide the pager until we know we need it
      $this->pnlPager->Visible = false;

      if(!empty($query))
      {

         $qstring = "SELECT O.iOrgID, O.vchName as OrgName, O.vchDescription, O.vchURL, O.vchSLURL, O.category_iCategoryID AS iCategoryID, C.vchName AS vchCategory FROM org O INNER JOIN category C ON O.category_iCategoryID = C.iCategoryID WHERE O.vchDescription LIKE '%" . mysql_real_escape_string($query) . "%' or O.vchName LIKE '%" . mysql_real_escape_string($query) . "%' or C.vchName LIKE '%" . mysql_real_escape_string($query) . "%' ORDER BY O.vchName";

         $DB->q_query->close();
         $DB->q_query->SQL = $qstring;
         $DB->q_query->open();

         if($DB->q_query->RecordCount > 10)
         {
            $records = GetSessionVar("records");
            if($DB->q_query->RecordCount <> $records)
            {
               SetSessionVar("records", $DB->q_query->RecordCount);
               // HACK: For some reason we can't set the pager's totalrecords
               // property except in the form create event. That fires before we
               // have the record count on first execution so we have to redirect
               // to ourselves to insure the session varialbe is populated and
               // assessible to the create function. If we could avoid the second
               // query execution we'd be in better shape but the current hack
               // requires that the query fire twice the first time a search
               // returning more than 10 records is performed.
               redirect("Search.php");
            }
            $this->pnlPager->Visible = true;
            $page = $this->pgResults->current_page;
         }

         if($DB->q_query->RecordCount > 0)
         {
            $results = "<P ALIGN='CENTER'><TABLE>";
            $results = $results . "<TR>";
            $results = $results . "<TD WIDTH='50%'>Category</TD>";
            $results = $results . "<TD WIDTH='50%'>Organization</TD>";
            $results = $results . "</TR>";

            // Skip records if the user has paged ahead in the results
            // HACK: This should be implemented on the server side to return
            // only the records necessary. Needs to be done yesterday so OK for
            // now. If I could trust VCL with heavy lifting this wouldn't be
            // necessary.
            for($i = 0; $i < (10 * ($page - 1)); $i++)
              $DB->q_query->next();

            $cnt = 0;
            while(!$DB->q_query->EOF && $cnt < 10)
            {
               $results = $results . "<TR>";
               $results = $results . "<TD><A HREF='Category.php?cid=" . $DB->q_query->iCategoryID . "'>" . $DB->q_query->vchCategory . "</A></TD>";
               $results = $results . "<TD><A HREF='OrgDetail.php?oid=" . $DB->q_query->iOrgID . "&src=search'>" . $DB->q_query->OrgName . "</A></TD>";
               $results = $results . "</TR>";
               $cnt = $cnt + 1;
               $DB->q_query->Next();
            }
            $results = $results . "</TABLE></P>";

            $this->lblResults->Caption = $results;
         }

         else
         {
            $results = "<P ALIGN='CENTER'><TABLE>";
            $results = $results . "<TR>";
            $results = $results . "<TD><STRONG>Your search produced no results. Please try again using a different search term.</STRONG></TD>";
            $results = $results . "</TR>";
            $results = $results . "</TABLE></P>";

            $this->lblResults->Caption = $results;
         }

         $DB->q_query->close();

      }

   }

   function btnSearchClick($sender, $params)
   {

      SetSessionVar("query", $this->edSearch->Text);

   }

}

global $application;

global $Search;

//Creates the form
$Search = new Search($application);

//Read from resource file
$Search->loadResource(__FILE__);

//Shows the form
$Search->show();

?>